package com.anjie.powerproject.controller;

import com.anjie.powerproject.entity.Manager;
import com.anjie.powerproject.service.impl.ManagerJLServiceImpl;
import com.anjie.powerproject.service.impl.ManagerServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("manager")
public class ManagerJLController {
    @Autowired
    private ManagerJLServiceImpl managerService;

    /**
     * 用户登录
     * @param request
     * @return
     */
    @PostMapping("/checkManager")
    @ResponseBody
    public Map checkAuthentication(HttpServletRequest request){
        Map res = new HashMap();
        String managerName = request.getParameter("managerName");
        String managerPwd = request.getParameter("managerPwd");

        Manager manager1 = managerService.findManagerByManagerNameAndManagerPwd(managerName,managerPwd);
        if(manager1 != null && manager1.getRemark() != null){
            String[] stationCodes = manager1.getRemark().split(",");
            for (String s:stationCodes){
                if("2".equals(s)){
                    res.put("message","账户存在且有操作权限，可执行操作！");
                    res.put("managerName",manager1.getManagerName());
                    res.put("status","1");
                    request.getSession().setAttribute("loginManager",manager1.getManagerName());
                    return res;
                }
            }
        }
        res.put("message","账户不存在或者没有管理权限！");
        res.put("status","0");
        return res;
    }

    @GetMapping("/judegManager")
    @ResponseBody
    public Map managerJudge(HttpServletRequest request){
        Map map = new HashMap();
        if(request.getSession().getAttribute("loginManager") == null){
            map.put("msg","暂时没有登录。");
            map.put("status","0");
            return map;
        }
        map.put("msg","已经登录成功。");
        map.put("status","1");
        map.put("managerName",request.getSession().getAttribute("loginManager"));
        return map;
    }

    /**
     * 用户退出登录
     * @param request
     * @return
     */
    @RequestMapping("/loginOut")
    @ResponseBody
    public String managerLoginOut(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute("loginManager");
        return "loginOut";
    }
}
